/*
 * CS_REQ_INVEN_USE_UPGRADE_STONE.cpp
 *
 *  Created on: 2013. 9. 30.
 *      Author: mega@gamevil.com
 */

#include "../../Command.h"

#include "../../User.h"
#include "../../NLogic/Bag.h"
#include "../../NLogic/Item.h"

#include "../../GameLog.h"

namespace ProjectZ { namespace NCommand {

void SendPacket_CS_REQ_INVEN_USE_UPGRADE_STONE(User * pUser, int16_t res, uint64_t itemSeq)
{
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();

	*pAck << res << itemSeq;

	pAck->MakePacket(CMD_SC_REQ_INVEN_USE_UPGRADE_STONE);
	pUser->Send(pAck);
}

void Command::CS_REQ_INVEN_USE_UPGRADE_STONE(User * pUser, ::Network::Packet * pPacket)
{
	PREPARE_PACKET;

	uint64_t	stoneSeq	= 0;
	uint64_t	itemSeq		= 0;
	char		itemBagType	= 0;
	char		stoneEquipPos	= 0;

	*pPacket >> stoneSeq >> itemSeq >> itemBagType >> stoneEquipPos;

	NLogic::Bag * pBagMisc = pUser->GetBag(static_cast<INVEN_BAG_TYPE>(BAG_TYPE_ACCESSORY));
	if (NULL == pBagMisc)
	{
		DEBUGLOG("CS_REQ_INVEN_USE_UPGRADE_STONE : pBagMisc is NULL!!");
		SendPacket_CS_REQ_INVEN_USE_UPGRADE_STONE(pUser, ACK_UNKNOWN_ERROR, itemSeq);
		return;
	}

	NLogic::Bag *pBag = pUser->GetBag(static_cast<INVEN_BAG_TYPE>(itemBagType));
	if (NULL == pBag)
	{
		DEBUGLOG("CS_REQ_INVEN_USE_UPGRADE_STONE : pBag is NULL!! bagType=%d", itemBagType);
		SendPacket_CS_REQ_INVEN_USE_UPGRADE_STONE(pUser, ACK_UNKNOWN_ERROR, itemSeq);
		return;
	}

	NLogic::Item * pStone = pBagMisc->GetItem(stoneSeq);
	if (NULL == pStone)
	{
		DEBUGLOG("CS_REQ_INVEN_USE_UPGRADE_STONE : pStone is NULL!! seq=%ld", stoneSeq);
		SendPacket_CS_REQ_INVEN_USE_UPGRADE_STONE(pUser, ACK_UNKNOWN_ERROR, itemSeq);
		return;
	}

	NLogic::Item * pItem = pBag->GetItem(itemSeq);
	if (NULL == pItem)
	{
		DEBUGLOG("CS_REQ_INVEN_USE_UPGRADE_STONE : pItem is NULL!!");
		SendPacket_CS_REQ_INVEN_USE_UPGRADE_STONE(pUser, ACK_UNKNOWN_ERROR, itemSeq);
		return;
	}

	if (pItem->GetData().open_upgrade_stone_slot <= 0)
	{
		DEBUGLOG("CS_REQ_INVEN_USE_UPGRADE_STONE : pItem->GetData().open_upgrade_stone_slot <= 0");
		SendPacket_CS_REQ_INVEN_USE_UPGRADE_STONE(pUser, ACK_UNKNOWN_ERROR, itemSeq);
		return;
	}

	if (pItem->GetData().quality < ITEM_QUALITY_D)
	{
		DEBUGLOG("CS_REQ_INVEN_USE_UPGRADE_STONE : pItem->GetData().quality < ITEM_QUALITY_D");
		SendPacket_CS_REQ_INVEN_USE_UPGRADE_STONE(pUser, ACK_UNKNOWN_ERROR, itemSeq);
		return;
	}

	bool bStoneEquip = false;

	for (int i = 3; i < 6; i++)
	{
		if (pItem->GetData().eff_type[i] == -1
				&& pItem->GetData().eff_pos[i] == -1
				&& pItem->GetData().eff_value[i] == -1)
		{
			pItem->GetData().eff_type[i]	= pStone->GetData().eff_type[1];
			pItem->GetData().eff_pos[i]		= pStone->GetData().eff_pos[1];
			pItem->GetData().eff_value[i]	= pStone->GetData().eff_value[1];
			bStoneEquip = true;
			break;
		}
	}

	if (false == bStoneEquip)
	{
		DEBUGLOG("CS_REQ_INVEN_USE_UPGRADE_STONE : false == bStoneEquip");
		SendPacket_CS_REQ_INVEN_USE_UPGRADE_STONE(pUser, ACK_UNKNOWN_ERROR, itemSeq);
		return;
	}

	pBagMisc->PopItem(stoneSeq);
	pStone->DeleteDatabaseItemInfo(NLogic::Item::ItemStatus::EM_ITEM_STATUS_ITEM_USE);

	pItem->UpdateDatabaseItemInfo();

	NGameLog::LogItemInfo(pUser, USE_UPGRADE_STONE_ITEM, 1, 0, pItem);

	SendPacket_CS_REQ_INVEN_USE_UPGRADE_STONE(pUser, ACK_OK, itemSeq);
}

} /*NCommand*/ } /*ProjectZ*/
